var menudragstart = new Array();
function menudrag(menuobj, e, op) {
	if(op == 1) {
		if(in_array(is_ie ? event.srcElement.tagName : e.target.tagName, ['TEXTAREA', 'INPUT', 'BUTTON', 'SELECT'])) {
			return;
		}
		menudragstart = is_ie ? [event.clientX, event.clientY] : [e.clientX, e.clientY];
		menudragstart[2] = parseInt(menuobj.style.left);
		menudragstart[3] = parseInt(menuobj.style.top);
		doane(e);
	} else if(op == 2 && menudragstart[0]) {
		var menudragnow = is_ie ? [event.clientX, event.clientY] : [e.clientX, e.clientY];
		menuobj.style.left = (menudragstart[2] + menudragnow[0] - menudragstart[0]) + 'px';
		menuobj.style.top = (menudragstart[3] + menudragnow[1] - menudragstart[1]) + 'px';
		/* iframe跟随  */
		divSetVisible(menuobj.id,true);
		doane(e);
	} else if(op == 3) {
		menudragstart = [];
		doane(e);
	}
}

function ebygum(eventobj) {
	if(!eventobj || is_ie) {
		window.event.cancelBubble = true;
		return window.event;
	} else {
		if(eventobj.target.type == 'submit') {
			eventobj.target.form.submit();
		}
		eventobj.stopPropagation();
		return eventobj;
	}
}

var userAgent = navigator.userAgent.toLowerCase();
var is_opera = userAgent.indexOf('opera') != -1 && opera.version();
var is_ie = (userAgent.indexOf('msie') != -1 && !is_opera) && userAgent.substr(userAgent.indexOf('msie') + 5, 3);

function in_array(needle, haystack) {
	if(typeof needle == 'string' || typeof needle == 'number') {
		for(var i in haystack) {
			if(haystack[i] == needle) {
					return true;
			}
		}
	}
	return false;
}

//弹出层
function showPop(name,myWidth,myTop,showBack){
	var numargs = arguments.length;     // 获取被传递参数的数值。 
	var moveFlag = true;
	if(numargs == 2){
		moveFlag = arguments[1];
	}
	//alert(moveFlag);
	//var expargs = showPop.length;       // 获取期望参数的数值。 
	//alert(numargs+"/"+expargs);
	
	var msgObj=document.getElementById(name);
	msgObj.onclick = ebygum;
	if(moveFlag){
		msgObj.onmousedown = function(event) {try{menudrag(msgObj, event, 1);}catch(e){}};
		document.body.onmousemove = function(event) {try{menudrag(msgObj, event, 2);}catch(e){}};
		msgObj.onmouseup = function(event) {try{menudrag(msgObj, event, 3);}catch(e){}};
	}
	if(myTop){
		msgObj.style.top=myTop+'px';

	}else{
		msgObj.style.top=(document.body.scrollTop?document.body.scrollTop:document.documentElement.scrollTop)+150+'px';
	}
	msgObj.style.display="";
	if(myWidth){
		var myCoordinate =returnCoordinate(myWidth)
		msgObj.style.left=myCoordinate+"px";
	}
	if(showBack){
	
	}else{
		resetOverlay(name);
		window.onresize = function(){resetOverlay(name);}//改变窗体重新调整位置
		window.onscroll = function(){resetOverlay(name);}//滚动窗体重新调整位置
		divSetVisible(name,true);
	}


}

//遮罩层
function resetOverlay(e){
    var a = document.getElementById(e);

    //去客户端左上坐标，宽，高
    var scrollLeft = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
    var scrollTop = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
    var clientWidth = document.documentElement.clientWidth;
    var clientHeight = document.documentElement.clientHeight;

	var bo = document.getElementById('BOX_overlay');
    //判断是否新建遮罩层,为空新建层
    if (bo==null){ 
        bo = document.createElement("div");
        bo.setAttribute('id','BOX_overlay');
        a.parentNode.appendChild(bo);
    }
	bo.className = 'overlay'; //样式
    bo.style.left = scrollLeft+'px';
    bo.style.top = scrollTop+'px';
    bo.style.width = clientWidth+'px';
    bo.style.height = clientHeight+'px';
    bo.style.display = "block";
    
    divSetVisible(e,true);
}

//取消弹出层
function cancelPop(name){
	divSetVisible(name,false);
	var msgObj=document.getElementById(name);
	msgObj.style.display="none";
	
	//取消后把遮罩层关掉
	if(document.getElementById('BOX_overlay')){
		document.getElementById('BOX_overlay').style.display="none";
	}
	window.onscroll = null;
    window.onresize = null;
}

// iframe跟随
function divSetVisible(popDivName,state){
	var IfrRef = document.getElementById('DivShim');
	/* */
	//<iframe id="DivShim" src="javascript:false;" scrolling="no" frameborder="0" style="position:absolute; top:0px; left:0px; display:none;">
	//</iframe>
	if(IfrRef != null){
		var DivRef = document.getElementById(popDivName);
		if(state){
			DivRef.style.display = "block";
			IfrRef.style.width = DivRef.offsetWidth;
			IfrRef.style.height = DivRef.offsetHeight;
			IfrRef.style.top = DivRef.style.top;
			IfrRef.style.left = DivRef.style.left;
			IfrRef.style.zIndex = DivRef.style.zIndex - 1;
			IfrRef.style.display = "block";
		}
		else{
			DivRef.style.display = "none";
			IfrRef.style.display = "none";
		}
	}
}
// 返回位置
function returnCoordinate(myWidth){
	W=document.body.clientWidth;
	X=(W-myWidth)/2;
	return X;
}